import * as api from '../../api/api.js';
import { NEWS_UAID,NEWS_PAID,ASSETS } from '../../config.js';
const app = getApp();
const os = app.os;

Page({
  data: {
    appLogo: ASSETS.logo,
    uaid: NEWS_UAID,
    paid: NEWS_PAID,
    isLogin: false,
    isLoading: false,
    isAgreed: false
  },

  onLoad: function (options) {
    this.setData({isLogin: app.checkLogin()});
  },
  
  login: function() {
    const that = this;
    if(this.data.isLoading === true){
      return;
    }

    if (!this.data.isAgreed) {
      app.Tips({
        title: '请先同意并阅读以下协议和政策',
        icon: 'none'
      });
      return;
    }

    that.setData({isLoading: true});
    let data = app.getLogsData('app', 2);

    os.login({
      force: true,
      success(res) {
        data.code = res.code;
        api.userLogin(data).then(ret => {
          that.setData({isLoading: false});
          if (ret.code == 200) {
            // console.log(ret)
            app.checkSession();
            app.updateUserInfo(ret.data);
            app.Tips({
              title: '登录成功',
              icon: "success"
            },{tab: 3, url: 1});
          }else{
            app.Tips({
              title: ret.msg,
              icon: "fail"
            });
          }
        }).catch(err => {
          
        });
      },
      fail(res) {
        that.setData({isLoading: false});
        app.Tips({
          title: '登录失败',
          icon: 'fail'
        });
      },
    });
  },

  onAgreementChange: function(){
    this.setData({
      isAgreed: !this.data.isAgreed
    })
  },

  handleCancel: function (){
    os.switchTab({
      url: '/pages/index/index'
    });
  },

})